package com.cxrj.zhsq.civilianManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.civilianManage.dao.CivilianMapper;
import com.cxrj.zhsq.civilianManage.dao.CivilianTypeMapper;
import com.cxrj.zhsq.civilianManage.dao.LegalServiceMapper;
import com.cxrj.zhsq.civilianManage.entity.Civilian;
import com.cxrj.zhsq.civilianManage.entity.CivilianType;
import com.cxrj.zhsq.civilianManage.entity.LegalService;
import com.cxrj.zhsq.civilianManage.service.CivilianService;
import com.cxrj.zhsq.utils.AddressUtil;
import com.cxrj.zhsq.utils.SqlCondition;

import net.sf.json.JSONObject;

@Service
public class CivilianServiceImpl implements CivilianService {
	@Resource
	private CivilianMapper civilianMapper;
	@Resource
	private CivilianTypeMapper civilianTypeMapper;
	@Resource
	private LegalServiceMapper legalServiceMapper;

	@Override
	public Map<String, Object> seleteCivilianPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> sqlMap = SqlCondition.getBaseCondition(request, response);
		int count = civilianMapper.selectCount(sqlMap);
		List<Civilian> list = civilianMapper.seleteCivilianPage(sqlMap);
		resultMap.put("total", count);
		resultMap.put("recordsFiltered", count);
		resultMap.put("rows", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertCivilian(Civilian civilian) {
		Map<String, Object> resultMap = new HashMap<>();
		int a = civilianMapper.selectByCode(civilian.getsCode());
		if (a > 0) {
			resultMap.put("result", 2);
			return resultMap;
		}
		civilian.setSid(UUID.randomUUID().toString());
		civilian.setsTime(new Date());
		civilian.setsState(0);
		int b = civilianMapper.insertSelective(civilian);
		resultMap.put("result", b);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateCivilian(Civilian civilian) {
		Map<String, Object> resultMap = new HashMap<>();
		int a = civilianMapper.updateByPrimaryKeySelective(civilian);
		resultMap.put("result", a);
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteCivilian(String code) {
		Map<String, Object> resultMap = new HashMap<>();
		Integer c1 = civilianMapper.deleteCivilian(code);
		civilianTypeMapper.deleteCivilianType(code);
		resultMap.put("result", 0);
		if (c1 > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteCivilianTypePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> sqlMap = SqlCondition.getBaseCondition(request, response);
		int count = civilianTypeMapper.selectCount(sqlMap);
		List<CivilianType> list = civilianTypeMapper.seleteCivilianTypePage(sqlMap);
		resultMap.put("total", count);
		resultMap.put("recordsFiltered", count);
		resultMap.put("rows", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteCivilianAll() {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> sqlMap = new HashMap<>();
		resultMap.put("list", civilianMapper.seleteCivilianAll(sqlMap));
		return resultMap;
	}

	@Override
	public Map<String, Object> insertCivilianType(CivilianType civilianType) {
		Map<String, Object> resultMap = new HashMap<>();
		int a = civilianTypeMapper.selectByCode(civilianType.getCode());
		if (a > 0) {
			resultMap.put("result", 2);
			return resultMap;
		}
		civilianType.setId(UUID.randomUUID().toString());
		civilianType.setCreateTime(new Date());
		civilianType.setState("0");
		int b = civilianTypeMapper.insertSelective(civilianType);
		resultMap.put("result", b);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateCivilianType(CivilianType civilianType) {
		Map<String, Object> resultMap = new HashMap<>();
		int a = civilianTypeMapper.updateByPrimaryKeySelective(civilianType);
		resultMap.put("result", a);
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteCivilianType(String id) {
		Map<String, Object> resultMap = new HashMap<>();
		int a = civilianTypeMapper.deleteByPrimaryKey(id);
		resultMap.put("result", a);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectByCivilianCode(String code, String status) {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> sqlMap = new HashMap<>();
		sqlMap.put("code", code);
		sqlMap.put("status", status);
		resultMap.put("list", civilianTypeMapper.seleteCivilianTypeByCode(sqlMap));
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteCivilianByStatus() {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> sqlMap = new HashMap<>();
		sqlMap.put("status", "1");
		resultMap.put("list", civilianMapper.seleteCivilianAll(sqlMap));
		return resultMap;
	}

	@Override
	public Map<String, Object> selectLegalService(String code, String dataIndex, String pageCount) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("code", code);
		sqlMap.put("start", Integer.parseInt(dataIndex));
		sqlMap.put("length", Integer.parseInt(pageCount));
		List<LegalService> list = legalServiceMapper.selectLegalService(sqlMap);
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectLegalServicePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> sqlMap = SqlCondition.getBaseCondition(request, response);
		int count = legalServiceMapper.selectCount(sqlMap);
		List<LegalService> list = legalServiceMapper.selectLegalServicePage(sqlMap);
		resultMap.put("total", count);
		resultMap.put("recordsFiltered", count);
		resultMap.put("rows", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertAndUpdateLegalService(LegalService legalService) {
		Map<String, Object> resultMap = new HashMap<>();
		Integer c = 0;
		if (!legalService.getId().equals("")) {
			c = legalServiceMapper.updateByPrimaryKeySelective(legalService);
		} else {
			legalService.setId(UUID.randomUUID().toString());
			legalService.setCreateTime(new Date());
			legalService.setStatus("1");
			c = legalServiceMapper.insertSelective(legalService);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectLegalServiceById(String id) {
		Map<String, Object> resultMap = new HashMap<>();
		LegalService legalService = legalServiceMapper.selectByPrimaryKey(id);
		String url = "http://api.map.baidu.com/geocoder/v2/?address=" + legalService.getS1() + "&output=json&ak=Pe44jCEWdEGa4LN8t6Mta0AQGUu1dZer";
		String json = AddressUtil.loadJSON(url);
		JSONObject obj = JSONObject.fromObject(json);
		if (obj.get("status").toString().equals("0")) {
			double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
			double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
			resultMap.put("x", lng);
			resultMap.put("y", lat);
		} else {
			resultMap.put("x", 113.9568800000);
			resultMap.put("y", 30.9178600000);
		}
		resultMap.put("legalService", legalService);
		return resultMap;
	}

	@Override
	public Map<String, Object> updateLegalService(String id) {
		Map<String, Object> resultMap = new HashMap<>();
		LegalService legalService = new LegalService();
		legalService.setId(id);
		legalService.setStatus("-1");
		Integer c = legalServiceMapper.updateByPrimaryKeySelective(legalService);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}
}
